<template>
	<div class="system-dept-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="deptDialogFormRef" :model="state" size="default" :rules="state.rules" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="上级部门">
							<el-cascader
								:options="state.deptData"
								:props="{ checkStrictly: true, value: 'id', label: 'deptName' }"
								placeholder="请选择部门"
								clearable
								class="w100"
								v-model="state.ruleForm.deptSuperior"
							>
								<template #default="{ node, data }">
									<span>{{ data.deptName }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="部门名称" prop="ruleForm.deptName">
							<el-input v-model="state.ruleForm.deptName" placeholder="请输入部门名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="部门编号" prop="ruleForm.deptCode">
							<el-input v-model="state.ruleForm.deptCode" placeholder="请输入部门编号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="部门排序" prop="ruleForm.deptSort">
							<el-input v-model="state.ruleForm.deptSort" placeholder="请输入部门排序" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="部门状态"  prop="ruleForm.deptStatus">
								<el-radio-group v-model="state.ruleForm.deptStatus">
									<el-radio label="1">启用</el-radio>
									<el-radio label="0">不启用</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="部门描述" prop="ruleForm.deptDescribe">
							<el-input v-model="state.ruleForm.deptDescribe" type="textarea" placeholder="请输入部门描述" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit(deptDialogFormRef)" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemDeptDialog">
import { reactive, ref, onMounted } from 'vue';
import type { FormInstance } from 'element-plus'
import { deptApi } from './api';
import { ElMessage } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 引入 api 请求接口
const api = deptApi();

// 定义变量内容
const deptDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		deptSuperior: [] as any[], // 上级部门
		id:'',//主键id
		parentId:'',//上级部门id
		deptName: '', // 部门名称
		deptCode: '', // 部门编号
		deptSort: '', // 部门排序
		deptStatus: '1', // 部门状态  0-禁用 1-启用
		deptDescribe: '', // 部门描述
	},
	deptData: [] as RouteItems, // 部门数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	rules: {
		'ruleForm.deptName': { required: true, message: '请输入部门名称', trigger: 'blur' },
		'ruleForm.deptCode': { required: true, message: '请输入部门编号', trigger: 'blur' },
		'ruleForm.deptSort': { required: true, message: '请输入排序', trigger: 'blur' },
		'ruleForm.deptStatus': { required: true, message: '请选择是否启用', trigger: 'blur' },
	},
});

// 获取所有部门
const getAllDept = () => {
	api.getDeptList().then(
		function (res) {
			state.deptData = res.data;
		}
	)
};

// 打开弹窗
const openDialog = (type: string, row: any) => {
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改部门';
		state.dialog.submitTxt = '修 改';
		//从后台获取该部门的所有上级部门，用于回显
		api.getDeptSuperior({'id':row.id}).then(
			function (res) {
				state.ruleForm.deptSuperior =  res.data.reverse();
			}
		)
	} else {
		//新增时 主键id和父id置为空
		state.ruleForm.id = '';
		state.ruleForm.parentId = '';
		state.dialog.title = '新增部门';
		state.dialog.submitTxt = '新 增';
	}
	state.dialog.isShowDialog = true;
	state.dialog.type = type;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return
	formEl.validate((valid) => {
		if (valid) {
			// console.log('校验通过！')
			//获取上级部门数组中的最后一个即为该部门的上级部门
			if(state.ruleForm.deptSuperior.length > 0) {
				state.ruleForm.parentId = state.ruleForm.deptSuperior[state.ruleForm.deptSuperior.length-1];
			} else {
				//如果数组为空，则表示该部门为一级部门，上级部门为0
				state.ruleForm.parentId = '0';
			}
			api.save(state.ruleForm).then(
				(res) => {
					if (res.data.code === "000000") {
						ElMessage.success(res.data.message);
						getAllDept();//更新上级部门数据
						closeDialog(); // 关闭弹窗
						emit('refresh');
					} else {
						ElMessage.warning(res.data.message);
					}
				}
			)
		} else {
			// console.log('校验未通过！')
			return false
		}
	})
};

// 页面加载时
onMounted(() => {
	getAllDept();
});

// 暴露变量
defineExpose({
	openDialog,
});
</script>
